﻿using Dapper;
using DBOptionPro.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBOptionPro.DAL
{
    /// <summary>
    /// 标记数据库访问类(mysql)
    /// </summary>
    public class MarkDAL : IDisposable
    {
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="option">系统设置参数对象</param>
        public MarkDAL(SysOption option)
        {
            _SysOption = option;
            string conStr = $"Database={_SysOption.DBName};Data Source={_SysOption.IP};User Id={_SysOption.User};Password={_SysOption.Pwd};CharSet=utf8;port={_SysOption.Port}";
            _DBConnection = new MySqlConnection(conStr);
        }
        /// <summary>
        /// 系统设置参数对象
        /// </summary>
        private SysOption _SysOption;
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        protected readonly IDbConnection _DBConnection;

        /// <summary>
        /// 获取服务器上的所有标记数据进行本地缓存
        /// </summary>
        /// <returns></returns>
        public async Task<List<MarkInfo>> GetAllMarks()
        {
            string sql = "select code,name,remark from ly_tdd_mark";
            var ret = await _DBConnection.QueryAsync<MarkInfo>(sql);
            return ret.ToList();

        }

        public void Dispose()
        {
            _DBConnection?.Close();
        }
    }
}
